1345B - Card Constructions - CodeForces Solution


binary search brute force dp math *1100

Please click on ads to support us..

Python Code:

t = int(input())
for _ in range(t):
    n = int(input())
    c = 0
    def main(num):
        global c
        z = 2
        while num >= 2:
            if num-z < 0:
                c+=1
                return num
            elif num == 0:
                c+=1
                return num
            else:
                num-=z
                z+=3
        c+=1
        if num > 0 and num < 2:
            return num
        elif num == 2:
            c+=1
            return 0
        else:
            return num

    while n >= 2:
        n=main(n)
    
    print(c)

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int main() 
{
  int t; cin >>t;
  while (t--)
  {
    int n; cin >>n;
    int count=0;
    int x=1;
    while (n>1)
    {
      if (n<7)
      {count+=n/2;break;}
      else if (n==7||n==8)
      {count++;break;}
      
      int a=(n/3)*2;
      int b=sqrt(a)-x;
      int c=(b*(b+1)/2)*3;
      int d=(b+1)*2;
      n-=c+d;
      if (n<0)
      {n+=c+d;x++;}
      else
      {count++;x=1;}
    }
    cout <<count<<endl;
  }
  
  
  
  
  return 0;
}


Comments

Submit
0 Comments
More Questions

791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter